<template>
  <div style="height: 100%;">
    <generate-layout-form v-if="viewDragLayout"
                          :data="viewDragLayout"
                          :relationList="relationList"
                          :refreshMap="refreshMap"
                          :isShowInTab="isShowInTab"
                          :formOption="formOption"
                          :bpmOption="bpmOption"
                          :subViews="subViews"
                          :stores="stores"
                          :parameterValue="parameterValue"
                          @setPk="setPk"
                          @setRefresh="setRefresh"
                          style="height: 100%;"
                          @addFormInCurrentTab="addFormInCurrentTab"
                          @editFormInCurrentTab="editFormInCurrentTab"
                          @detailFormInCurrentTab="detailFormInCurrentTab"
                          v-on="$listeners">
    </generate-layout-form>
    <view-redirect-row v-else-if="layout && layout.length > 0"
                       :rows="layout"
                       :relationList="relationList"
                       :refreshMap="refreshMap"
                       :isShowInTab="isShowInTab"
                       :formOption="formOption"
                       :bpmOption="bpmOption"
                       :subViews="subViews"
                       :stores="stores"
                       :parameterValue="parameterValue"
                       @setPk="setPk"
                       @setRefresh="setRefresh"
                       style="height: 100%;"
                       @addFormInCurrentTab="addFormInCurrentTab"
                       @editFormInCurrentTab="editFormInCurrentTab"
                       @detailFormInCurrentTab="detailFormInCurrentTab"
                       v-on="$listeners">
    </view-redirect-row>

    <form-redirect-div ref="formInCurrent" :stores="stores"></form-redirect-div>
  </div>
</template>

<script>

  import { getAction } from '@/api/manage'
  import ViewRedirectRow from './ViewRedirectRow'
  import YchConstant from "@/api/YchConstant";
  import GenerateLayoutForm from '../layoutMaking/GenerateLayoutForm'
  import RelationUtil from "./RelationUtil";
  import RefreshUtil from "./RefreshUtil";
  import FormRedirectDiv from "./FormRedirectDiv";
  import {TabInCurrentMixin} from './TabInCurrentMixin'

  export default {
    name: 'ViewLayoutRedirect',
    mixins: [TabInCurrentMixin],
    components: {
      ViewRedirectRow,
      GenerateLayoutForm,
      FormRedirectDiv,
    },
    props: {
      viewCode: String,
      stores: {
        type: Object,
        default: () => {
          return {}
        }
      },
      parameterValue: {
        type: Array,
        default: () => []
      },
      isShowInTab: Boolean,
      formOption: Object,
      bpmOption: Object,
    },
    data () {
      return {
        relationList: [], // 联动关系
        refreshMap: {}, // 刷新关系
        layout: [],
        viewDragLayout: null,
        url: {
          getViewModel: YchConstant.PREFIX_EFORM + '/engine/viewEngine/getMainViewModel'
        },
        subViews: {},
      }
    },
    methods: {
      reLoadForPreview (viewDragLayout, model) {
        this.relationList = []
        this.refreshMap = {}
        this.subViews = {}
        this.layout = []
        this.viewDragLayout = null

        this.subViews = model.subViews
        this.relationList = model.relationList
        if (model.viewLayout && model.viewLayout.rows) {
          this.layout = model.viewLayout.rows
        } else if (model.viewDragLayout) {
          this.viewDragLayout = model.viewDragLayout
        }
      },
      reLoad() {
        this.relationList = []
        this.refreshMap = {}
        this.subViews = {}
        this.layout = []
        this.viewDragLayout = null

        if (!this.viewCode) {
          return
        }
        getAction(this.url.getViewModel, {viewCode: this.viewCode}).then((res) => {
          if (res.success) {
            const model = res.result
            this.subViews = model.subViews
            this.relationList = model.relationList
            if (model.viewLayout && model.viewLayout.rows) {
              this.layout = model.viewLayout.rows
            } else if (model.viewDragLayout) {
              this.viewDragLayout = model.viewDragLayout
            }
          } else {
            this.messageUtil.warning(res.message)
          }
        }).finally(() => {
        })
      },
      setPk(option) {
        this.relationList = RelationUtil.setChangeValue(this.relationList, option)
      },
      setRefresh(option) {
        this.refreshMap = RefreshUtil.setRefreshValue(this.refreshMap, option)
      },
      doResize() {
        // 解决缓存路由的情况下，切换路由后，自由组合页面宽度变成0的问题
        if (this.viewDragLayout && this.$route.meta.keepAlive) {
          this.$nextTick(() => {
            // 手动触发窗口resize事件
            if (document.createEvent) {
              const event = document.createEvent("HTMLEvents");
              event.initEvent("resize", true, true);
              window.dispatchEvent(event);
            } else if (document.createEventObject) {
              window.fireEvent("onresize");
            }
          })
        }
      }
    },
    watch: {
      viewCode: {
        handler () {
          this.reLoad()
        },
        immediate: true
      }
    },
    activated() {
      this.doResize();
    }
  }
</script>
